/**********************************************
 * 
 * File Name				:	RegisterCode.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-01 11:12:53 +0800 (星期六, 01 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 86 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/utils/RegisterCode.java $
 * 
 * ********************************************
 */

package com.busibuilding.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.busibuilding.common.Constants;

/**
 * Class Name: RegisterCode.java 
 * Description: Generate the rand code 
 * @author: YU Zhipeng
 * @version: 1.0
 * @since: 1.0
 * Date: 2009-07-09
 */

public class RegisterCode extends HttpServlet implements Constants{
	private static final long serialVersionUID = -1923870960707025530L;
	private static Logger logger = LogManager.getLogger(RegisterCode.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		try {
			response.reset();

			response.setContentType("image/png");

			BufferedImage image = new BufferedImage(70, 20,
					BufferedImage.TYPE_INT_RGB);

			Graphics g = image.getGraphics();

			g.setColor(this.getRandColor(230, 250));

			g.fillRect(0, 0, 70, 20);

			Random r = new Random();
			for (int i = 0; i < 133; i++) {
				g.setColor(this.getRandColor(150, 210));
				int x1 = r.nextInt(70);
				int y1 = r.nextInt(20);
				int x2 = r.nextInt(12);
				int y2 = r.nextInt(12);
				g.drawLine(x1, y1, x2 + x1, y2 + y1);
			}

			String regCode = "";
			// g.setFont(new Font("Times New Roman",Font.BOLD,18));
			g.setFont(new Font("Arial", Font.BOLD, 18));

			for (int i = 0; i < 5; i++) {
				g.setColor(this.getRandColor(50, 150));
				int num = this.getCode();
				char temp;
				if (num > 10) {
					temp = (char) num;
					regCode += temp;
					g.drawString(String.valueOf(temp), 13 * i + 6, 16);
				} else {
					regCode += num;
					g.drawString(String.valueOf(num), 13 * i + 6, 16);
				}

			}
			logger.debug("Generate rand code = " + regCode);
			request.getSession().setAttribute(SESSION_RAND_CODE, regCode);

			ServletOutputStream sos = response.getOutputStream();
			ImageIO.write(image, "PNG", sos);
			sos.close();
		} catch (IOException e) {
			logger.fatal(e);
		} catch (Exception e) {
			logger.fatal(e);
		}

	}

	private Color getRandColor(int low, int high) {
		Random ran = new Random();
		int r = low + ran.nextInt(high - low);
		int g = low + ran.nextInt(high - low);
		int b = low + ran.nextInt(high - low);

		return new Color(r, g, b);
	}

	private int getCode() {
		/*
		 * 1 : 0-9 2 : a-z 3 : A-Z
		 */
		Random r = new Random();
		int code = 0;

		int flag = r.nextInt(3);
		if (flag == 0) {
			code = r.nextInt(10);
		} else if (flag == 1) {
			code = getCodeByScope('a', 'z');
		} else {
			code = getCodeByScope('A', 'Z');
		}
		return code;
	}

	private int getCodeByScope(int low, int high) {
		Random ran = new Random();
		return low + ran.nextInt(high - low);

	}

}
